package com.dingwei.superiordiary.mvp.presenter;

import android.text.TextUtils;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;
import com.luck.picture.lib.entity.LocalMedia;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.List;

/**
 * Created by lihao on 2017/9/12.
 */

public class ReturnGoodsPresenter extends basePresenter {

    private Contract.ReturnGoodsView returnGoodsView;

    public ReturnGoodsPresenter(Contract.ReturnGoodsView view) {
        super(view);
        this.returnGoodsView = view;
    }

    public void service(String id, String description, List<LocalMedia> photoList,String type) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.PAY_SERVICE);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id", id);
        rp.addBodyParameter("type", type);
        rp.addBodyParameter("description",description);
        for (int i = 0; i < photoList.size(); i++) {
            LocalMedia photoItem = photoList.get(i);
            if (!TextUtils.isEmpty(photoItem.getCompressPath())) {
                File file = new File(photoItem.getCompressPath());
                if (file != null && file.exists()) {
                    rp.addBodyParameter("file" + (i + 1), file);
                }
            }
        }
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                returnGoodsView.onSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                returnGoodsView.onFail();
            }
        });

    }
}
